package user.controllers;

import helpers.CookieHelper;
import helpers.EmailHelper;
import helpers.IdUniqueHelper;
import helpers.NumberHelper;
import helpers.UrlHelper;

import java.io.IOException;
import java.util.List;
import java.util.TreeMap;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import search.ArticleSearchEngine;
import search.ArticleSearchModel;
import sun.security.krb5.Config;
import systems.Recently;
import systems.RecentlyTag;
import user.forms.ArticleForm;
import user.forms.CaptchaForm;
import user.models.ArticleModel;
import user.models.CategoryModel;

import debugs.ArticleDebug;

import entities.ApplicationConfig;
import entities.Article;
import entities.ArticleSearch;
import entities.Category;
import entities.User;

public class ArticleAddServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException {
		
		ApplicationConfig appConfig = (ApplicationConfig) request.getAttribute("sysConfig");
		
		//Get User
		User user = (User) request.getAttribute("user_logged_in");
		
		if (user == null) {
			String cookieSubmit = CookieHelper.getCookie(request, "isLoggin");
			
			if (cookieSubmit == null || cookieSubmit.isEmpty()) {
				response.sendRedirect("/confirm-login");
				return;
			}
		}
		
		String url = (String) request.getParameter("url");
		
		TreeMap check = new TreeMap();
		
		String language = (String) request.getAttribute("language");
		if (language == null || language.isEmpty()) {
			language = "en";
		}
		
		CategoryModel categoryModel = CategoryModel.getInstance();
		List<Category> categories = categoryModel.getAll(appConfig.getApplicationId());
		
		Article article = new Article();
		article.setLanguage(language);
		
		
		request.setAttribute("check", check);
		request.setAttribute("article", article);
		request.setAttribute("categories", categories);
		request.setAttribute("url", url);
		request.setAttribute("user", user);
		try {
			request.getRequestDispatcher("/user/pages/article_form.jsp").forward(request, response);
		} catch (ServletException e) {
			e.printStackTrace();
		}
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException {
		
		ApplicationConfig appConfig = (ApplicationConfig) request.getAttribute("sysConfig");
		
		String url = (String) request.getParameter("url");
		
		String language = (String) request.getAttribute("language");
		if (language == null || language.isEmpty()) {
			language = "en";
		}
		
		CategoryModel categoryModel = CategoryModel.getInstance();
		List<Category> categories = categoryModel.getAll(appConfig.getApplicationId());
		
		ArticleModel articleModel = ArticleModel.getInstance();
		Article article = new Article();
		article.setLanguage(language);
		
		User user = (User) request.getAttribute("user_logged_in");
		if (user != null) {
			article.setCreator(user.getId());
		} 
		
		TreeMap form = ArticleForm.checkValid(request, article);
		TreeMap check = (TreeMap) form.get("checkValid");
		article = (Article) form.get("article");
		
		if (user == null) {
			check = CaptchaForm.checkValid(request, check);
		}
		
		if (check.size() == 0) {
			article.setStatus(1);
			
			article.setId(IdUniqueHelper.getId());
			article.setRate(NumberHelper.getRandomNumberFrom(2, 5));
			article.setAuthKey(IdUniqueHelper.getId());
			articleModel.insert(appConfig.getApplicationId(), article);
			request.setAttribute("success", "Submit article success with id " + article.getId());
			
			//Set search article
			ArticleSearch articleSearch = ArticleSearch.convert(article);
			ArticleSearchEngine.update(articleSearch);
			
			//Update tag
			RecentlyTag.insert(appConfig.getApplicationId(), article.getKeywords());
			
			//Update recently Cache
			if (article.getStatus() == 1) {
				Recently.update(appConfig.getApplicationId(), article);
			}
			
			article = new Article();
		}
		
		request.setAttribute("check", check);
		request.setAttribute("article", article);
		request.setAttribute("categories", categories);
		request.setAttribute("url", url);
		request.setAttribute("user", user);
		try {
			request.getRequestDispatcher("/user/pages/article_form.jsp").forward(request, response);
		} catch (ServletException e) {
			e.printStackTrace();
		}
	}
}
